﻿using isky.dapper;
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace isky.Plugin
{
  public class QuoteComparisonActionPlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the execution context service from the service provider.
            var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            // Obtain the Organization Service factory service from the service provider
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            var service = factory.CreateOrganizationService(context.UserId);

            string InputAreaId = context.InputParameters["InputAreaId"].ToString() ?? "";
            string InputProduct = context.InputParameters["InputProduct"].ToString() ?? "";
            string InpuSart = context.InputParameters["InpuSart"].ToString() ?? "";
            string InputEnd = context.InputParameters["InputEnd"].ToString() ?? "";
            try
            {
                QuoteComparsionDal quoteComparsionDal = new QuoteComparsionDal();

                string result = quoteComparsionDal.getQuery(InputAreaId,InputProduct, InpuSart, InputEnd);
                context.OutputParameters["OutputArgument"] = result;
                string cost= quoteComparsionDal.getSumCostTotaldifference(InputAreaId, InputProduct, InpuSart, InputEnd);
                context.OutputParameters["OutCost"] = cost;
            }
            catch (Exception ex)
            {
                context.OutputParameters["OutputArgument"] = ex.Message;
            }

        }
       
    }
}
